<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="fontsize">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!--  -->
      <!-- alert警告 -->
      <el-alert
        title="注意：只允许为第三级分类设置相关参数！"
        type="warning"
        show-icon
        :closable="false"
      >
      </el-alert>

      <!-- 选择商品分类区域 -->
      <el-row class="cat_opt">
        <el-col>
          <span>选择商品分类：</span>
          <!-- 选择商品分类的级联选择框 -->
          <el-cascader
            expandTrigger="hover"
            v-model="selectedCateKeys"
            :options="cateList"
            :props="cateProps"
            @change="handleChange"
          ></el-cascader>
        </el-col>
        <el-col></el-col>
      </el-row>

      <!-- 动态参数和静态参数 -->
      <!-- @tab-click="handleClick" -->
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <!-- 动态参数 -->
        <el-tab-pane label="动态参数" name="many" ref="move">
          <el-row>
            <el-button
              size="mini"
              type="primary"
              :disabled="isButtonDisabled"
              @click="open"
              >添加参数</el-button
            >
          </el-row>

          <!-- form表单 -->
          <el-table :data="manyTableData" style="width: 100%" border stripe>
            <!-- 展开的符号列表 -->
            <el-table-column label="" width="50px" type="expand">
            </el-table-column>

            <!-- 索引列 -->
            <el-table-column label="#" type="index" width="50px">
            </el-table-column>

            <!-- 参数名称 -->
            <el-table-column label="参数名称" prop="attr_name">
            </el-table-column>

            <!-- 修改删除 -->
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="primary"
                  icon="el-icon-edit"
                  @click="bianji(scope.row)"
                  >编辑</el-button
                >
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  @click="removeParams(scope.row.attr_id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>

        <!-- 静态属性 -->
        <el-tab-pane label="静态属性" name="only" ref="quiet">
          <el-row>
            <el-button
              size="mini"
              type="primary"
              :disabled="isButtonDisabled"
              @click="quiet"
              >添加属性</el-button
            >
          </el-row>

          <!-- form表单 -->
          <!-- :data="tableData" -->
          <el-table :data="onlyTableData" style="width: 100%" border stripe>
            <!-- 展开的符号列表 -->
            <el-table-column label="" width="50px" type="expand">
            </el-table-column>

            <!-- 索引列 -->
            <el-table-column label="#" type="index" width="50px">
            </el-table-column>

            <!-- 参数名称 -->
            <el-table-column label="参数名称" prop="attr_name">
            </el-table-column>

            <!-- 修改删除 -->
            <el-table-column label="操作"> </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>

      <!-- 添加参数或属性对话框 -->
      <el-dialog
        :title="'添加' + moveQuiet"
        :visible.sync="addDialogVisible"
        width="50%"
        @close="addDialogClosed"
      >
        <!-- 添加表单 -->
        <el-form
          :model="addForm"
          :rules="addFormRules"
          ref="addFormRef"
          label-width="100px"
        >
          <el-form-item :label="moveQuiet" prop="attr_name">
            <el-input v-model="addForm.attr_name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addParams">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 修改参数或属性对话框 -->
      <el-dialog
        :title="'修改' + moveQuiet"
        :visible.sync="editDialogVisible"
        width="50%"
        @close="editDialogClosed"
      >
        <!-- 添加表单 -->
        <el-form
          :model="editForm"
          :rules="editFormRules"
          ref="editFormRef"
          label-width="100px"
        >
          <el-form-item :label="moveQuiet" prop="attr_name">
            <el-input v-model="editForm.attr_name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editParams">确 定</el-button>
        </span>
      </el-dialog>

      <!-- /卡片 -->
    </el-card>
  </div>
</template>

<script>
import {
  getCateList,
  paramsgetCateList,
  AddMoveQuiet,
  EditSubmission,
  delThreeParameter,
} from "@/api/commodity";
export default {
  name: "",
  components: {},
  props: {},
  data() {
    return {
      // 添加属性和添加参数的禁用状态
      // isButtonDisabled: true,
      activeName: "many",
      //分类列表
      cateList: [],
      //用户在级联下拉菜单中选中的分类id
      selectedCateKeys: [],
      // 配置级联菜单的数据如何显示
      cateProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      //用来保存动态参数数据
      manyTableData: [],
      //用来保存静态属性数据
      onlyTableData: [],

      //控制添加参数.属性对话框的显示或隐藏
      addDialogVisible: false,
      //添加参数的表单数据对象
      addForm: {
        attr_name: "",
      },

      //添加表单验证规则
      addFormRules: {
        attr_name: [{ required: true, message: "请输入名称", trigger: "blur" }],
      },

      // 动态参数和静态属性的label值
      moveQuiet: "",

      // 动态编辑的表单显示隐藏
      editDialogVisible: false,

      //修改参数.属性对话框中的表单
      editForm: {
        attr_name: "",
        attr_id: null,
      },

      //修改表单的验证规则
      editFormRules: {
        attr_name: [{ required: true, message: "请输入名称", trigger: "blur" }],
      },
    };
  },
  computed: {
    //添加计算属性用来获取按钮禁用与否
    // isButtonDisabled() {
    //   return this.selectedCateKeys.length !== 3;
    // },
    // 获取级联菜单用户选择的 第三级的数据id
    cateId() {
      if (this.selectedCateKeys.length === 3) {
        return this.selectedCateKeys[this.selectedCateKeys.length - 1];
      } else {
        return null;
      }
    },
    // 判断选择的数据是不是第三级的数据
    isButtonDisabled() {
      return this.selectedCateKeys.length !== 3;
    },
  },
  watch: {},
  created() {
    this.getCateList();
  },
  methods: {
    // 选择商品分类级联菜单发生变化时触发的方法
    async handleChange(value) {
      // console.log("params级联菜单选中ID:", value);

      if (value.length < 3) {
        return;
      }

      const res = await paramsgetCateList(this.cateId, this.activeName);
      // console.log("根据用户选择的三级分类和面板获取参数数据:", res);

      if (this.activeName === "many") {
        // 那么就是动态的数据
        this.manyTableData = res;
      } else if (this.activeName === "only") {
        // 静态的属性
        this.onlyTableData = res;
      }
    },

    // 获取所有分类的方法
    async getCateList() {
      // console.log("点击添加触发获取数据");
      const res = await getCateList();
      // console.log("params所有分类:", res);
      this.cateList = res;
    },

    //  form 表单动态参数和静态属性发生改变触发
    handleClick() {
      // console.log("11111:", this.activeName);
      // this.handleChange();
      this.handleChange(this.selectedCateKeys);
    },

    // 关闭添加数据对话框
    addDialogClosed() {
      //当关闭修改参数.属性对话框时
      this.$refs.addFormRef.resetFields();
    },

    open() {
      const movelabel = this.$refs.move.label;
      // console.log("动态参数的label:", thislabel);
      this.moveQuiet = movelabel;
      this.addDialogVisible = true;
    },

    quiet() {
      // console.log("点击添加动态或者静态:", label);
      const quietlabel = this.$refs.quiet.label;
      // console.log("动态参数的label:", this.$refs.quiet.label);
      this.moveQuiet = quietlabel;
      this.addDialogVisible = true;
    },

    // 点击确定  添加动态 / 静态属性
    addParams() {
      this.$refs.addFormRef.validate(async (isOk) => {
        if (!isOk) {
          // 校验不成功 直接return
          return;
        }

        try {
          // 校验成功  请求发起添加添加分类 方法
          // console.log("成功过");
          await AddMoveQuiet(this.cateId, {
            attr_name: this.addForm.attr_name,
            attr_sel: this.activeName,
            attr_vals: "a,b,c",
          });
          // console.log("点击添加请求数据成功:", res);
          this.$message.success("添加" + this.moveQuiet + "数据成功");
          this.addDialogVisible = false;
          // this.getCateList();
          this.handleChange(this.selectedCateKeys);
        } catch (error) {
          this.$message.error("添加" + this.moveQuiet + "失败");
          // console.log(error);
        }
      });
    },

    // 点击编辑按钮
    bianji(value) {
      // console.log("编辑:", value);
      this.editForm.attr_name = value.attr_name;
      this.editForm.attr_id = value.attr_id;
      // 判断
      if (value.attr_sel === "many") {
        // console.log("322:是动态参数", this.$refs.move);
        this.moveQuiet = this.$refs.move.label;
      }
      this.editDialogVisible = true;
    },

    // 编辑对话框的 取消 / 关闭
    editDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },

    // 点击了编辑框的确定按钮
    editParams() {
      this.$refs.editFormRef.validate(async () => {
        // console.log("点击了编辑框的确定按钮", isOk);
        // console.log(this.cateId);
        // console.log(this.editForm.attr_id);
        try {
          await EditSubmission(this.cateId, this.editForm.attr_id, {
            attr_name: this.editForm.attr_name,
            attr_sel: this.activeName,
          });

          this.$message.success("修改" + this.$refs.move.label + "数据成功");
          this.editDialogVisible = false;
          this.handleChange(this.selectedCateKeys);
        } catch (error) {
          // console.log();
          this.$message.error(error.response);
        }
      });
    },

    // 点击删除按钮 动态参数的删除事件
    removeParams(attr_id) {
      // console.log(attr_id);
      this.$confirm("请问是否要删除该" + this.activeName, "删除提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // this.$message({
          //   type: "success",
          //   message: "删除成功!",
          // });
          // console.log(this.cateId);
          // console.log(this.editForm.attr_id);
          // this.editForm.attr_id =
          try {
            await delThreeParameter(this.cateId, attr_id);
            // console.log("删除数据:", res);
            this.handleChange(this.selectedCateKeys);
          } catch (error) {
            console.log(error);
          }
        })
        .catch(() => {
          return this.$message.info("取消删除");
        });
    },
  },
};
</script>

<style scoped lang="less">
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-alert {
  margin-bottom: 15px;
}
.fontsize {
  font-size: 14px;
}
</style>
